
package com.apes.fn.crm.ReturnVisit.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.AccessProvider;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.model.Person;
import com.apes.scm.masterdata.party.repository.AccessProviderRepository;
import com.apes.scm.masterdata.party.repository.PersonRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 售后回访历史
 *
 * @author bbyford
 * @create 2020-2-5
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VisitHistory.CONST_TABLE_NAME)
public class VisitHistory extends Model {
    public static final String CONST_TABLE_NAME = "fn_visit_history";

    @Id
    @Comment("编码")
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = "fn_visit_history", name = "售后回访历史", prefix = "HH%(yy)%(month)", padding = 5, useDateRange=true)
    private String id;

    @NotNull
    @Comment("回访单号")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "return_visit_id", referencedColumnName = "id", nullable = false)
    private ReturnVisit returnVisit;

    @States(name = "单据状态", values = {
            @State(id = "again", name = "继续回访"),
            @State(id = "end", name = "终止"),
            @State(id = "finish", name = "回访完成")})
    @Column(name = "state", length = 16, nullable = false)
    private String state = "again";

    @States(name = "服务满意度", values = {
            @State(id = "-2", name = "非常不满意"),
            @State(id = "-1", name = "不满意"),
            @State(id = "0", name = "一般"),
            @State(id = "1", name = "满意"),
            @State(id = "2", name = "非常满意")})
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "visit_satisfaction_id", referencedColumnName = "id")
    private BusinessAttributeValue visitSatisfaction;

    @Comment("不满意原因")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "visit_dissatisfaction_reason", referencedColumnName = "id")
    private Enumeration visitDissatisfactionReason;

    @Comment("回访失败原因")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "visit_fail_reason", referencedColumnName = "id")
    private Enumeration visitFailReason;


    @Comment("建议")
    @Column(name = "advice", length = 4000)
    private String advice;

    @Comment("备注")
    @Column(name = "remark", length = 4000)
    private String remark;

    @Comment("不回访标记")
    @Column(name = "not_visit_sign", columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    private boolean notVisitSign = false;

    @Comment("回访时间段")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "visit_period", referencedColumnName = "id")
    private Enumeration visitPeriod;

    @Comment("回访开始时间")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "start_visit_time", nullable = false)
    private Date startVisitTime;

    @Comment("回访结束时间")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "end_visit_time", nullable = false)
    private Date endVisitTime;

    @Comment("下次回访日期")
    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "next_visit_date")
    private Date nextVisitDate;


    @Comment("回访人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "visited_id", referencedColumnName = "id", nullable = false)
    private PartyRole visitedId;

    @Comment("回访日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "visited_date", nullable = false)
    private Date visitedDate;

    @Transient
    private Branch visitBranch; // 回访部门

    public Branch getVisitBranch() {
        Person person = SpringManager.getBean(PersonRepository.class).findOne(
                JpaDsl.toCriteriaByEq(
                        "party", visitedId.getParty()
                )
        ).orElse(null);
        return  person != null ? person.getWorkBranch() : null;
    }


    @Transient
    private Dept dept; // 业务部门

    public Dept getDept() {
        return returnVisit.getDept();
    }

    @Transient
    private AccessProvider accessProvider; //接入商

    public AccessProvider getAccessProvider() {
        if( returnVisit == null || returnVisit.getCompany() == null ) return null;
        return SpringManager.getBean(AccessProviderRepository.class).findByCompany(
                returnVisit.getCompany().getId()
        );
    }
}


